<script setup>
    import {useMouseInElement} from '@vueuse/core'
        import { watch, ref } from 'vue';
    // 动态传入imageList图片
        defineProps({
            imageList: {
                type: Array,
                default() {
                    return []
                }
            }
        })
    // // 图片列表
    // const imageList = [
    // 'https://yanxuan-item.nosdn.127.net/d917c92e663c5ed0bb577c7ded73e4ec.png',
    // 'https://yanxuan-item.nosdn.127.net/e801b9572f0b0c02a52952b01adab967.jpg',
    // 'https://yanxuan-item.nosdn.127.net/b52c447ad472d51adbdde1a83f550ac2.jpg',
    // 'https://yanxuan-item.nosdn.127.net/f93243224dc37674dfca5874fe089c60.jpg',
    // 'https://yanxuan-item.nosdn.127.net/f881cfe7de9a576aaeea6ee0d1d24823.jpg'
    // ]
    // 小图切换大图
    const currentIndex = ref(0)
    function LiEnter(index) {
        // console.log(index);
        currentIndex.value = index
    }

    // 获取鼠标在model内位置
    const target = ref(null)
    // console.log(target);

    const {elementX, elementY, isOutside} = useMouseInElement(target)
    
    const left = ref(0)
    const top = ref(0)

    const positionX = ref(0)
    const positionY = ref(0)
    watch([elementX, elementY], () => {
        // console.log('xy变化了');
        // 鼠标不移入盒子， 不执行后续逻辑
        if (isOutside.value) return
        // console.log('后续逻辑执行了');
        // 控制滑块距离
        if (elementX.value > 100 && elementX.value < 300) {
            left.value = elementX.value - 100
        }
        if (elementY.value > 100 && elementY.value < 300) {
            top.value = elementY.value - 100
        }

        // 处理边界 
        if (elementX.value < 100) {left.value = 0}
        if (elementX.value > 300) {left.value = 200}
        
        if (elementY.value < 100) {top.value = 0}
        if (elementY.value > 300) {top.value = 200}

        // 大图位置
        positionX.value = -left.value * 2
        positionY.value = -top.value * 2
    })
</script>


<template>
    <!-- 图片预览区 -->
    <div class="goods-image">
        <div class="middle" ref="target">
            <img :src="imageList[currentIndex]" alt="">
            <div v-show="!isOutside" class="layer" :style="{left: `${left}px`, top: `${top}px`}"></div>
        </div>
        <ul class="small">
            <li v-for="item, index in imageList"
                :key="item" @mouseenter="LiEnter(index)"
                :class="{active: index === currentIndex}">
                <img :src="item" alt="">
            </li>
        </ul>
        <div v-show="!isOutside" class="big" :style="[{
            backgroundImage: `url(${imageList[currentIndex]})`,
            backgroundPositionX: `${positionX}px`,
            backgroundPositionY: `${positionY}px`
        }]"></div>
    </div>
</template>


<style scoped lang='scss'>
    .goods-image{
        position: relative;
        display: flex;
        width: 480px;
        height: 400px;
        .middle{
            position: relative;
            width: 400px;
            height: 400px;
            .layer{
                width: 200px;
                height: 200px;
                background: rgba(0, 0, 0, .2);
                position: absolute;
                left: 0;
                top: 0;
            }
        }
        .small{
            width: 80px;
            li{
                width: 68px;
                height: 68px;
                margin-left: 12px;
                margin-bottom: 15px;
                cursor: pointer;
                &:hover,
                &.active{
                    border: 2px solid $xtxColor;
                }
                
            }
        }
        .big{
            position: absolute;
            left: 412px;
            top: 0;
            width: 400px;
            height: 400px;
            background-size: 800px 800px;
            background-repeat: no-repeat;
            z-index: 9;
        }
    }
</style>